package copyengine.utils.tick.node
{
	import copyengine.resource.lazyload.CELazyLoadCacheContainer;
	import copyengine.resource.lazyload.CELazyLoadMovieClipeContainer;
	import copyengine.utils.GeneralUtils;
	import copyengine.utils.cache.object.ICEMovieClip;
	import copyengine.utils.debug.CELog;

	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;

	public class OldAnimationTickObjectNode extends TickObjectNode
	{
		//结束后直接返回 不做任何操作
		public static const END_ACTION_DO_NOTHING:int = 0;
		//结束后删除Target
		public static const END_ACTION_REMOVE:int = 1;
		//结束后隐藏Target
		public static const END_ACTION_INVISIBLE:int = 2
		//结束后Stop住Target
		public static const END_ACTION_STOP:int = 3;

		private var isStartTick:Boolean;
		private var endAction:int;
		private var target:DisplayObjectContainer;

		private var playCount:int;
		private var playTotalFrame:int;

		public function OldAnimationTickObjectNode(_target:DisplayObjectContainer, _callBackFunction:Function, _repeatTime:int, _endAction:int)
		{
			super(_callBackFunction, 1, _repeatTime);
			target = _target;
			endAction = _endAction;
			playCount = 1;
			playTotalFrame = 0;

			if (target is CELazyLoadMovieClipeContainer)
			{
				isStartTick = false;
				(target as CELazyLoadMovieClipeContainer).addLoadComplateCallBackFunction(startTickLogic);
			}
			//Cache的Movieclip会在其内部自己播放Mc,不再受AnimationNode的控制
			else if (target is ICEMovieClip)
			{
				isStartTick = false;
				(target as CELazyLoadCacheContainer).addMovieClipPlayFinishedCallBackFunction(onCacheMovieClipePlayFinish);
			}
			else
			{
				startTickLogic();
			}
		}

		private function onCacheMovieClipePlayFinish():void
		{
			isStartTick = true;
		}


		private function startTickLogic():void
		{
			isStartTick = true;
			if (target is MovieClip)
			{
				playTotalFrame = (target as MovieClip).totalFrames;
			}
			else if (target is ICEMovieClip)
			{
				playTotalFrame = (target as ICEMovieClip).totalFrames;
			}
		}

		override protected function tickLogic(_elapseTime:int):void
		{
			if (isStartTick)
			{
				if (target is MovieClip)
				{
					(target as MovieClip).gotoAndStop(playCount);
				}
				playCount++;
				if (playCount > playTotalFrame)
				{
					playCount = 1;
					super.tickLogic(_elapseTime);
				}
			}
		}

		override public function destory():void
		{
			super.destory();
			switch (endAction)
			{
				case END_ACTION_DO_NOTHING:
					break;
				case END_ACTION_REMOVE:
					GeneralUtils.removeTargetFromParent(target);
					break;
				case END_ACTION_STOP:
					if (target is MovieClip)
					{
						(target as MovieClip).stop();
					}
					else if (target is ICEMovieClip)
					{
						//Cache的元素停留在最后一帧
						(target as ICEMovieClip).gotoAndStop(playTotalFrame);
					}
					break;
				case END_ACTION_INVISIBLE:
					if (target is MovieClip)
					{
						(target as MovieClip).stop();
					}
					else if (target is ICEMovieClip)
					{
						//Cache的元素停留在最后一帧
						(target as ICEMovieClip).gotoAndStop(playTotalFrame);
					}
					target.visible = false;
					break;
			}
			target = null;
		}
	}
}
